<script>
  import {
    appConfig,
    envConfig
  } from './config/index.js'
  import {
    appversion
  } from './api/common/login'
  import {
    NoticeTemplates
  } from './pages/index/noticeNavigate.js'
  let env_value = 'prod'
  // #ifdef APP-PLUS
  const env_v = uni.getStorageSync('env_v')
  if (env_v) {
    env_value = env_v
  }
  // #endif
  // #ifdef H5
  env_value = appConfig.env
  // #endif
  const tragetEnv = envConfig[env_value]

  export default {
    globalData: {
      env: env_value,
      // requestUrl: tragetEnv.apiHost + appConfig.apiBaseUrl,
      requestUrl: tragetEnv.apiHost + '/api/wvms',
      requestImgUrl: tragetEnv.imgUrl,
      // latitude: undefined,
      // longitude: undefined,
    },
    onLaunch: function(params) {
      // #ifdef APP-PLUS
      plus.screen.unlockOrientation()
      plus.screen.lockOrientation('portrait-primary')
      // this.getAppLocation(); // 华为审核问题,暂时删除
      plus.push.addEventListener(
        'click',
        function(msg) {
          console.log(msg)
          NoticeTemplates(msg.payload.type, msg.payload)
        },
        false,
      )
      plus.push.addEventListener(
        'receive',
        (msg) => {
          console.log(msg)
        },
        false,
      )
      // #endif
    },
    methods: {
      getAppLocation() {
        var that = this
        uni.getLocation({
          type: 'wgs84',
          success: function(mapLocation) {
            // that.$scope.globalData.requestLng = mapLocation.longitude;
            // that.$scope.globalData.requestLat = mapLocation.latitude;
            // that.$scope.globalData.requestTime = Date.now();
          },
        })
      },
      getVersion() {
        var updateNow = uni.getStorageSync('updateNow')
        appversion({}).then((res) => {
          var serverVersion = res.result.version.split('.').join('') //服务器版本
          // 更新关闭状态下不弹窗
          if (res.result.status == 2) {
            return false
          }
          appversion({}).then((res) => {
            plus.runtime.getProperty(plus.runtime.appid, function(info) {
              var systemVersion = info.version.split('.').join('') //本地版本
              console.log('👆服务器版本APP:' + serverVersion + '      ' + '👇本地版本APP:' + systemVersion)
              if (Number(serverVersion) > Number(systemVersion) && updateNow != 1) {
                console.log('需要更新，跳转首页发起更新')
                uni.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          })
        })
      },
    },
    onShow() {
      var that = this
      setTimeout(() => {
        var pages = getCurrentPages()
        // console.log(pages)
        if (pages.length > 0) {
          var currentRoute = pages[pages.length - 1].route
          if (currentRoute != 'pages/index/index') {
            that.getVersion()
          }
        }
      }, 0)
    },
  }
</script>

<style lang="less">
  // 字体图标
  @import '@/styles/fonts.less';
  @import '@/styles/app.less';

  /* #ifdef APP-PLUS */
  page {
    font-family: PingFang SC-Regular, PingFang SC;
  }

  /* #endif */
</style>
